const express = require('express')
const router = express.Router()
const multer = require('multer')

const {
    searchDishes,
    editDish,
    uploadDish,
    selectDishes,
} = require('../db/model/dishModel/dishModel')


/* 
 *查询所有菜品
 *get方法
 *无参数
 *
 */
router.get('/searchDishes', (req, res) => {
    const result = searchDishes()
    return result.then(val => {
        if (val.length !== 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                dishList: val
            })
        } else {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        }
    })
})


/**
 * 按需查看查询所有菜品
 * post请求
 * 参数：菜品类型：dishType
 * 
 */
router.post('/selectDishes', (req, res) => {
    let {
        dishType
    } = req.body
    const result = selectDishes(dishType)
    return result.then(val => {
        if (val.length !== 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                dishList: val
            })
        } else {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        }
    })
})


/* 
 *修改菜品信息
 *post方法
 *参数：dishId,dishName，originalPrice，presentPrice，isVaild，dishType，dishIntroduce
 *
 */
router.post('/editDish', (req, res) => {
    let {
        dishId,
        dishName,
        originalPrice,
        presentPrice,
        isVaild,
        dishType,
        dishIntroduce
    } = req.body
    const result = editDish(dishId, dishName, originalPrice, presentPrice, isVaild, dishType, dishIntroduce)
    return result.then(val => {
        if (val) {
            res.send({
                code: 200,
                msg: '修改成功'
            })
        } else {
            res.send({
                code: -1,
                msg: '修改失败'
            })
        }
    })
})


/**
 * 上传菜品
 * post请求
 * 参数：dishName,imgUrl,originalPrice,presentPrice,isVaild,dishType,dishIntroduce
 * 
 */
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        // 指定文件路径
        cb(null, './upload/images')
    },
    filename: function (req, file, cb) {
        // 指定文件名
        //文件名重复覆盖
        // 后缀名发生改变
        console.log('----', file)
        let exts = file.originalname.split('.')
        let ext = exts[exts.length - 1]
        let tmpname = (new Date()).getTime() + parseInt(Math.random() * 9999)
        cb(null, `${tmpname}.${ext}`)
    }
});
var upload = multer({
    storage: storage
});

router.post('/upload', upload.single('imgUrl'), (req, res) => {
    console.log(req.file)
    console.log(req.body)
    let {
        dishName,
        originalPrice,
        presentPrice,
        isVaild,
        dishType,
        dishIntroduce
    } = req.body
    let imgUrl = `/public/images/${req.file.filename}`
    const result = uploadDish(dishName, imgUrl, originalPrice, presentPrice, isVaild, dishType, dishIntroduce)
    return result.then(val => {
        if (val) {
            res.send({
                code: 200,
                msg: '添加成功'
            })
        } else {
            res.send({
                code: -1,
                msg: '添加失败'
            })
        }
    })
})



module.exports = router